/*
 * 文件名：BillPayResponce.java	 
 * 时     间：下午1:44:57
 * 作     者：lyf      
 * 版     权： 2012-2022 湖南智软网络科技, 公司保留所有权利.
 * 联     系：http://www.073586.com/
 */
package com.message.handler.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.context.ApplicationContext;

import com.cz073586.tools.util.JsonUtil;
import com.cz073586.tools.util.Tools;
import com.local.common.CommonContains;
import com.local.model.CarParkIn;
import com.local.model.LocalCoupon;
import com.local.model.LocalPayBill;
import com.local.model.LocalPayBillCopy;
import com.local.service.face.LocalCouponCashService;
import com.local.service.face.LocalCouponService;
import com.local.service.face.ParkwellPayBillService;
import com.message.entity.BillInfo;
import com.message.entity.PayInfo;
import com.message.entity.TransDataVo;
import com.message.handler.face.ResponceHandler;
import com.qianhai.common.constants.CommonContans;

/**
 * 类描述:这里是类描述
 * 
 * @ClassName: BillPayResponce
 * @author Administrator
 * @date 2015年10月11日 下午1:44:57
 */
public class PayInfoResponce implements ResponceHandler
{
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public TransDataVo resoponse(ApplicationContext srpingApp,
			TransDataVo transData)
	{
		TransDataVo result = new TransDataVo<BillInfo>();
		if (!Tools.isEmpty(transData.getData())
				&& transData.getData() instanceof Map)
		{
			
			String dataObjec = JsonUtil.getJsonString(transData.getData());
			BillInfo billInfo = JsonUtil.fromJson(dataObjec, BillInfo.class);
			
			if (!Tools.isEmpty(billInfo.getCaroNo()))
			{
				try
				{
					ParkwellPayBillService payBill = srpingApp
							.getBean(ParkwellPayBillService.class);
					CarParkIn cpIn = payBill.selectCarNo(billInfo.getCaroNo());
					if (!Tools.isEmpty(cpIn))
					{
						//商家电子优惠券service
						LocalCouponCashService localCouponCashService = srpingApp
								.getBean(LocalCouponCashService.class);
						
						//查询已经支付的流水
						List<LocalPayBillCopy> listbillListCopy = payBill.queryListByCarInUuid(cpIn.getUuid());
						List<LocalPayBill> listbillList=new ArrayList<LocalPayBill>();
						if(!Tools.isEmpty(listbillListCopy)&&listbillListCopy.size()!=0){
							for(LocalPayBillCopy lpCopy:listbillListCopy){
								LocalPayBill localPayBill=new LocalPayBill();
								BeanUtils.copyProperties(lpCopy, localPayBill);
								listbillList.add(localPayBill);
							}
						}
						
						/*// 返回商家抵用劵集合  ,//查询抵用劵 ,状态为1 的所有集合
						List<LocalCouponCash> li = localCouponCashService.queryCouponCashByCarNo(cpIn.getCarNo());*/
						
						// 返回流水装换集合
						List<PayInfo> payInfoLs = retParkwellPayBill(listbillList,srpingApp);
						
						// 返回
						BillInfo billInfoResult = new BillInfo();
						billInfoResult.setCaroNo(billInfo.getCaroNo());
						billInfoResult.setPayInfoLs(payInfoLs);
						result.setData(billInfoResult);
						result.setResult(CommonContans.STRUTS_SUCCESS);
						return result;
					}
				} catch (Exception e)
				{
					result.setResult(CommonContans.STRUTS_ERROR);
					result.setErrorCode("10002-1234-241-2341");
					result.setErrorMsg("未知异常");
				}
			}
		}
		return result;
	}
	
	public List<PayInfo> retParkwellPayBill(List<LocalPayBill> listbillList,
			ApplicationContext srpingApp)
	{
		try
		{
			List<PayInfo> payInfoLs = new ArrayList<PayInfo>();
			if (!Tools.isEmpty(listbillList) && listbillList.size() > 0)
			{
				for (LocalPayBill pb : listbillList)
				{
					// 流水表，装换payInfo
					PayInfo pi = new PayInfo();
					pi.setPayTime(!Tools.isEmpty(pb.getPayTime()) ? pb
							.getPayTime().getTime() : new Date(System
							.currentTimeMillis()).getTime());
					pi.setPayType(!Tools.isEmpty(pb.getPayType()) ? pb.getPayTypeInt(pb.getPayType()) : CommonContains.PAY_TYPE_OTHER);
					pi.setPrice(!Tools.isEmpty(pb.getPrice()) ? (int)(pb.getPrice()*100) : 0);
					
					LocalCouponService localCouponService = srpingApp
							.getBean(LocalCouponService.class);
					List<LocalCoupon> pCouponLs = localCouponService
							.queryByBillUuid(pb.getPayBillUuid());
					if (!Tools.isEmpty(pCouponLs))
					{
						for (LocalCoupon pc : pCouponLs)
						{
							PayInfo pic = new PayInfo();
							pic.setPayTime(!Tools.isEmpty(pc.getPayTime()) ? pc
									.getPayTime().getTime() : new Date(System
									.currentTimeMillis()).getTime());
							pic.setPayType(CommonContains.PAY_TYPE_OTHER);
							pic.setPrice(!Tools.isEmpty(pc.getCouponPrice()) ? (int)(pc
									.getCouponPrice()*100) : 0);
							payInfoLs.add(pic);
						}
					}
					payInfoLs.add(pi);
				}
				
			}
			/*if (!Tools.isEmpty(li))
			{
				for (LocalCouponCash pcCash : li)
				{
					PayInfo payInfo = new PayInfo();
					payInfo.setPayTime(new Date(System.currentTimeMillis()).getTime());
					payInfo.setPayType(CommonContains.PAY_TYPE_COUPON_CASH);
					payInfo.setPrice(!Tools.isEmpty(pcCash.getPrice()) ? pcCash.getPrice().intValue() : 0);
					payInfoLs.add(payInfo);
				}
			}*/
			return payInfoLs;
			
		} catch (Exception e)
		{
			// TODO: handle exception
			return null;
		}
	}
}
